package com.jason.randomgame.mycompnents;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.jason.randomgame.R;

public class WheelView extends SurfaceView implements SurfaceHolder.Callback {

	private float screenHight, screenWidth;// 
	private float radius;// 
	private float circleRadius; // 
	private float startAngle;// 
	private float sweepAngle; // 	
	private float speed; // 	
	private float acceleration; // 
	private int itemCount;
	private int[] itemColor;
	private String[] titleString;
	private double[] ratioArray;
	private Paint mPaint;
	private Canvas mCanvas;
	private Path path;
	private SurfaceViewThread myThread;
	private SurfaceHolder holder;
	private boolean done = false;
	private boolean surfaceExist = false;
	private boolean rotateEnabled = false;
	private boolean clockwise = true;
	private Bitmap mBackground;

	public WheelView(Context context, AttributeSet attr) {
		super(context, attr);
		initial();
	}

	@SuppressLint("ParserError")
	public void initial() {
		this.setZOrderOnTop(true);
		this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
		holder = getHolder();
		holder.addCallback(this);

		mPaint = new Paint();
		
		

		itemColor = new int[] {
				Color.RED,// 
				Color.LTGRAY,// 
				Color.CYAN,// 								
				Color.GREEN,// 				
				Color.MAGENTA // 
		};
		
		itemCount = 5;
		titleString = new String[] { "去唱歌", "吃大餐", "看电影", "去逛街", "去睡觉" };
		ratioArray = new double[] { 20, 20, 20, 20, 20};
		radius = 120;
		circleRadius = 15;
		startAngle = 0;
		speed = 0;
	}

	public void setDirection(boolean bool, float sp) {
		clockwise = bool;
		speed = sp;
		acceleration = Math.abs(speed) / 100;
	}

	public void rotateEnable() {
		rotateEnabled = true;
	}

	public void rotateDisable() {
		rotateEnabled = false;
	}

	public void start() {
		if (myThread == null) {
			myThread = new SurfaceViewThread();
		}
		if (surfaceExist) {
			myThread.start();
		}
	}

	public void stopRotate() {
		if (myThread != null) {
			myThread = null;
			done = true;
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		if (myThread != null) {
			myThread.start();
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mBackground = BitmapFactory.decodeResource(getResources(), R.drawable.green);
		surfaceExist = true;
		screenHight = getHeight();
		screenWidth = getWidth();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		surfaceExist = false;
		myThread = null;
		done = true;
	}

	class SurfaceViewThread extends Thread {

		public SurfaceViewThread() {
		}

		public void updateView() {
			SurfaceHolder surfaceHolder = holder;
			mCanvas = surfaceHolder.lockCanvas();
			radius = screenWidth / 3;
			float f1 = screenWidth / 2;
			float f2 = (float) (screenHight / 2.5);
			if(mCanvas == null){
				return;
			}
			mCanvas.drawColor(Color.TRANSPARENT,Mode.CLEAR);
//			mCanvas.save();

//		    Paint paint = new Paint();                    
//		    paint.setStyle(Style.FILL);              
//		    paint.setColor(0xff009933);
//		    mCanvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), paint); 
////		    mCanvas.drawBitmap(mBackground, 0, 0, paint); 
//			mCanvas.save();
			
			Paint localPaint = new Paint();
			localPaint.setAntiAlias(true);
			localPaint.setStyle(Paint.Style.STROKE);// 			
			localPaint.setStrokeWidth(circleRadius); //
			RadialGradient radialGradient = new RadialGradient(f1, f2, radius
					+ circleRadius, new int[] { Color.GREEN, Color.GREEN,
					Color.GREEN, Color.YELLOW, Color.BLACK }, null,
					TileMode.MIRROR);// 
			localPaint.setShader(radialGradient);// 
			mCanvas.drawCircle(f1, f2, radius, localPaint);
			mCanvas.save();

			float f3 = f1 - radius;
			float f4 = f2 - radius;
			float f5 = f1 + radius;
			float f6 = f2 + radius;
			RectF rectF = new RectF(f3, f4, f5, f6);

			drawItem(rectF);

			Paint localPaint2 = new Paint();
			localPaint2.setAntiAlias(true);

			localPaint2.setStrokeWidth(5);
			localPaint2.setColor(Color.WHITE);
			mCanvas.drawLine(f1, f2 + radius / 5, f1, f2 - radius / 2,
					localPaint2);
			mCanvas.drawCircle(f1, f2, 12, localPaint2);
			mCanvas.save();

			
			if (rotateEnabled) {
				if (clockwise) {
					startAngle += speed;
					speed -= acceleration;
				} else {
					startAngle -= speed;
					speed -= acceleration;
				}
				if (speed <= 0) {
					rotateEnabled = false;
				}
			} else {
				startAngle -= 360;
			}
			surfaceHolder.unlockCanvasAndPost(mCanvas);
		}

		@SuppressLint("ParserError")
		public void drawText(RectF localRectf, float localStartAngle,
				float localSweepAngle, String str) {
			path = new Path();
			// float pading = (sweepAngle-str.length())/2;

			if (localSweepAngle > 0) {
				path.addArc(localRectf, localStartAngle, localSweepAngle);
			}
			path.addArc(localRectf, localStartAngle - localSweepAngle,
					-localSweepAngle);
			mPaint.setTextSize(getWidth()/9);
			mCanvas.drawTextOnPath(str, path, 20, -15, mPaint);
			mCanvas.save();
		}

		public void drawItem(RectF localRectf) {
			for (int i = 0; i < itemCount; i++) {
				mPaint.setColor(itemColor[i]);
				sweepAngle = (float) (360 * ratioArray[i] / 100);
				if (!clockwise) {
					sweepAngle = 0 - sweepAngle;
				}
				mCanvas.drawArc(localRectf, startAngle, sweepAngle, true,
						mPaint);
				mCanvas.save();
				drawText(localRectf, startAngle, sweepAngle, titleString[i]);
				startAngle += sweepAngle;
			}
		}

		@Override
		public void run() {
			super.run();
			mPaint.setAntiAlias(true);
			while (!done) {
				updateView();
			}
		}
	}
}